package com.ls.lserp.modules.order.service;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.modules.order.dao.BigConsumptionDao;
import com.ls.lserp.modules.order.entity.BigConsumption;
import com.ls.lserp.modules.sys.entity.Sequence;
import com.ls.lserp.modules.sys.utils.SequenceUtil;
import com.ls.lserp.modules.sys.utils.UserUtils;



/**
 * @ClassName: BigConsumptionService
 * @Description: 大货单耗 一览 新建 删除 修改
 * 
 * @author 
 * @date 2017年02月06日
 *
 */

@Service
@Transactional(readOnly = true)
public class BigConsumptionService extends CrudService<BigConsumptionDao, BigConsumption> {
	@Autowired

	// 分类方法接口
	private BigConsumptionDao bigConsumptionDao;

	/**
	 * @Title: findList
	 * @Description: 一览查询
	 * @param page
	 * @param BigConsumption
	 * @return Page<BigConsumption>
	 */
	public Page<BigConsumption> findList(Page<BigConsumption> page, BigConsumption bigConsumption) {
		
		// tab页分，检索条件设定(0:全部,1:新建,2:已保存)
		int tabFlg = bigConsumption.getTabPageStatus();
		if(1==tabFlg){
			bigConsumption.setStatus("1");
		}else if(2==tabFlg){
			bigConsumption.setStatus("2");
		}
		
		// 设置分页参数
		bigConsumption.setPage(page);
		
		// 取得查询数据
		List<BigConsumption> updateList = bigConsumptionDao.findList(bigConsumption);
		
		List<BigConsumption> specimenProductList = null;
		BigDecimal decTotalPrice = null;

		// 执行分页查询
		page.setList(updateList);
		return page;
	}

	 /**
	 * 查询一条数据 根据id查询面辅配料信息和单耗信息
	 * 
	 * @param user
	 * @return
	 */
    public BigConsumption findBigConsumption(BigConsumption bigConsumption){

    	List<BigConsumption> material = bigConsumptionDao.findOfferMaterialList(bigConsumption.getId());
    	bigConsumption = bigConsumptionDao.get(bigConsumption);
    	bigConsumption.setBigConsumptionMaterialList(material);
    	return bigConsumption;
    }
    
    /**
	 * 查询款式
	 * @param page
	 * @param user
	 * @return
	 */
    public Page<BigConsumption> findStyleList(Page<BigConsumption> page, BigConsumption bigConsumption) {
        // 生成数据权限过滤条件（dsf为dataScopeFilter的简写，在xml中使用 ${sqlMap.dsf}调用权限SQL）
        //designOption.getSqlMap().put("dsf", dataScopeFilter(user.getCurrentUser(), "o", "a"));
        // 设置分页参数
    	bigConsumption.setPage(page);
        // 执行分页查询
        page.setList(bigConsumptionDao.findStyleList(bigConsumption));
        return page;
    }
    
    /**
	 * ID查询一条款式信息
	 * 
	 * @param user
	 * @return
	 */
    public BigConsumption getStyle(BigConsumption bigConsumption){
    	
    	return bigConsumptionDao.getStyle(bigConsumption);
    } 
    
    /**
	 * 款式ID查询单耗信息
	 * 
	 * @param user
	 * @return
	 */
    public List<BigConsumption> findUnitCostList(Long id){
    	
    	return bigConsumptionDao.findUnitCostList(id);
    } 
    
    /**
	 * 款式ID查询面料信息
	 * 
	 * @param user
	 * @return
	 */
	 public BigConsumption findMaterialList(long id){
	    	
	    	List<BigConsumption> materialList = bigConsumptionDao.findStyleMaterialList(id);
	    	BigConsumption bigConsumption = new BigConsumption();
	    	
	    	bigConsumption.setBigConsumptionMaterialList(materialList);
	    	
	    	return bigConsumption;
	    } 
    /**
	 * ID查询一条单耗信息
	 * 
	 * @param user
	 * @return
	 */
    public BigConsumption getUnitCost(Long id){
    	
    	List<BigConsumption> materialList = bigConsumptionDao.findOfferMaterialList(id);
    	BigConsumption bigConsumption = new BigConsumption();
    	bigConsumption = bigConsumptionDao.getUnitCost(id);
    	bigConsumption.setBigConsumptionMaterialList(materialList);
    	
    	return bigConsumption;
    } 
	/**
	 * 新增/修改一条数据
	 * @param user
	 */
    @Transactional(readOnly = false)
    public void saveBigConsumption(BigConsumption bigConsumption) {
    	// 判断新建编辑
    			//若为null 执行插入语句（新建）
    			if (bigConsumption.getId()==null){
    				StringBuilder conCode = new StringBuilder();
    		        Sequence sequence=SequenceUtil.offerCode("20", "sequence_consumption", 2,bigConsumption.getStyleNo());
    		        conCode.append(sequence.getReturnCode());
    		        conCode.append(bigConsumption.getStyleNo());
    		        
    		        bigConsumption.setUnitCostNo(sequence.getReturnCode());
    		        bigConsumption.setCreateBy(UserUtils.getUser());
    		   
    		        bigConsumptionDao.insert(bigConsumption);
    		        long tempId = bigConsumption.getId();
    		        bigConsumption.setId(tempId);
    				bigConsumptionDao.insertUnitCostStyleR(bigConsumption);
    				
    				if (bigConsumption.getBigConsumptionMaterialList()!=null) {
    					bigConsumptionDao.insertMaterial(bigConsumption);
    				}
    			// 编辑
    			}else{
    				// 更新数据
    				bigConsumptionDao.update(bigConsumption);
    				bigConsumptionDao.updateUnitCostStyle(bigConsumption);
    				bigConsumptionDao.delete(bigConsumption);
    				if (bigConsumption.getBigConsumptionMaterialList()!=null) {
    					bigConsumptionDao.insertMaterial(bigConsumption);  					
    				}			
    		}			
    }
}
